package org.ne.banksys.service.serviceImpl;

import com.github.pagehelper.Page;
import org.ne.banksys.entity.Card;
import org.ne.banksys.entity.User;
import org.ne.banksys.mapper.CardMapper;
import org.ne.banksys.mapper.UserMapper;
import org.ne.banksys.service.UserService;
import org.ne.banksys.utils.MD5Utils;
import org.ne.banksys.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wey
 * @date 2025/6/13 下午10:43 06
 **/

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CardMapper cardMapper;

    @Override
    public List<User> getAll() {
        return userMapper.getAll();
    }

    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    public User findByUserPID(String PID) {
        return userMapper.findByUserPID(PID);
    }

    @Override
    public User findByUserPhone(String phone) {
        return userMapper.findByUserPhone(phone);
    }

    @Override
    public User findUserById(Long id) {
        return userMapper.findByCustomerId(id);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public void updateAddress(String address) {
        Map<String,Object> info = ThreadLocalUtils.get();
        long id = Long.parseLong(info.get("customerId").toString());
        userMapper.updateAddress(address, id);
    }

    @Override
    public void updatePwd(String newMd5) {
        Map<String,Object> info = ThreadLocalUtils.get();
        long id = Long.parseLong(info.get("customerId").toString());
        userMapper.updatePwd(newMd5, id);
    }

    @Override
    public void register(String username, String password, String PID, String phone) {
        String md5pass =  MD5Utils.code(password);
        userMapper.add(username, md5pass, PID, phone);
    }

    @Override
    public void delete(String PID, String username, String pass) {
        String md5pass =  MD5Utils.code(pass);
        Map<String,Object> info = ThreadLocalUtils.get();
        Long id = Long.parseLong(info.get("customerId").toString());
        List<Card> cards = cardMapper.selectByCustomerId(id);
        for (Card card : cards) {
             cardMapper.deleteById(card.getCardId());
        }
        userMapper.delete(PID, username, md5pass);
    }

    @Override
    public void delete(Long id) {
        List<Card> cards = cardMapper.selectByCustomerId(id);
        for (Card card : cards) {
            cardMapper.deleteById(card.getCardId());
        }
        userMapper.deleteById(id);
    }

    @Override
    public Page<User> getAllUsers(Integer page, Integer size) {
        if (page == 1) {
            page = 0;
        }
        return userMapper.getPage(page, size);
    }

    @Override
    public List<User> searchUserByUserName(String username) {
        return userMapper.searchByCustomerName(username);
    }


}
